﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public static Form1 Cform;   //标记功能选择窗口
        public Form1()
        {
            InitializeComponent();
        }
        //确定目标文件的路径
        private string SelectFile()
        {
            string filepath = string.Empty;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;      //该值确定是否可以选择多个文件
            openFileDialog.Title = "请选择文件";     //弹窗的标题
            openFileDialog.InitialDirectory = "D:\\";       //默认打开的文件夹的位置
            openFileDialog.Filter = "Files(*.cs*)|*.cs*";       //筛选文件
            openFileDialog.ShowHelp = true;     //是否显示“帮助”按钮

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filepath = openFileDialog.FileName;
            }
            else
            {
                return "File Path";
            }


            return filepath;
        }
        //读取目标文件的行数
        private int CountLine(string path)
        {
            FileStream fileStream = new FileStream(path,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite);
            StreamReader streamReader = new StreamReader(fileStream);
            int numberOfLine = 0;
            string nextline = streamReader.ReadLine();
            while (nextline != null)
            {
                numberOfLine++;
                nextline = streamReader.ReadLine();
            }
            return numberOfLine;
        }
        private int CountWord(string path)
        {
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader streamReader = new StreamReader(fileStream);
            int numberOfWord = 0;
            string str = streamReader.ReadToEnd();
            if (string.IsNullOrEmpty(str))
                return 0;
            str = str.Replace("\n", " ").Replace("\r", " ");

            streamReader.Close();
            fileStream.Close();
            return Regex.Matches(str, @"[A-Za-z][A-Za-z'-]*").Count;
        }
        private void Select_btn_Click(object sender, EventArgs e)
        {
            string filepath = SelectFile();
            label2.Text = filepath;
            label4.Text = Convert.ToString(CountLine(filepath)); 
            label6.Text = Convert.ToString(CountWord(filepath));
        }

        private void Delete(string path)
        {
            try
            {
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamReader reader = new StreamReader(fileStream);
                int numOfLine = CountLine(path);
                string str, strToWrite = string.Empty;
                for (int i = 0; i < numOfLine; i++)
                {
                    str = reader.ReadLine();
                    //判断是否为注释
                    bool isNote = Regex.IsMatch(str, @"\s*[/]{2,}");
                    if (str != string.Empty && !isNote)
                    {
                        strToWrite += str;
                        if (i != numOfLine - 1)
                        {
                            strToWrite += "\n";
                        }
                    }

                }
                //将修改后的字符串写入原文件中
                FileStream fileStream2 = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamWriter writer = new StreamWriter(fileStream2);
                writer.Write(strToWrite);
                writer.Flush();

                writer.Close();
                reader.Close();
                fileStream.Close();
                MessageBox.Show("删除成功！");
            }
            catch (Exception e)
            {
                MessageBox.Show("输入的路径有误！");
            }


        }
        private void delete_btn_Click(object sender, EventArgs e)
        {
            Delete(label2.Text);
            label4.Text = CountLine(label2.Text).ToString();
            label6.Text = CountWord(label2.Text).ToString();

        }
        public string getPath()
        {
            return label2.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Cform = this;
        }

        private void calculate_btn_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
            this.Hide();
        }
        //获取出现的单词及个数
        public List<Words> CountAcountAppearTimes(string path)
        {
            List<Words> list = new List<Words>();
            
            bool isNew = true;
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader reader = new StreamReader(fs);
            string str = reader.ReadToEnd();

            if (string.IsNullOrEmpty(str))
                return null;
            str = str.Replace("\n", " ").Replace("\r", " ");
            MatchCollection collection = Regex.Matches(str, @"[A-Za-z][A-Za-z'-]*");
            //遍历获取匹配到的各个单词
            foreach (Match mc in collection)
            {
                //遍历list判断单词是否为list中存储的单词
                foreach (Words wd in list)
                {
                    if (wd.getWord() == mc.Value)    //与list中单词匹配，则为重复出现，该单词出现次数加一
                    {
                        isNew = false;   //重复出现
                        wd.timesUp();   //出现次数加一
                        break;
                    }
                }
                if (isNew)    //若为首次出现，则新建对象存储该单词信息
                {
                    Words newWord = new Words(mc.Value, 1);
                    list.Add(newWord);
                }
                isNew = true;
            }
            return list;
        }
       
    }
}
